package cn.woochen.functionwidget.sample.player

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import cn.jzvd.Jzvd
import cn.jzvd.JzvdStd
import cn.woochen.functionwidget.R
import kotlinx.android.synthetic.main.view_player.view.*


/**
 * 这里可以监听到视频播放的生命周期和播放状态
 * 所有关于视频的逻辑都应该写在这里
 * Created by Nathen on 2017/7/2.
 */
class PlayerView : JzvdStd {
    constructor(context: Context) : super(context) {}

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    override fun init(context: Context) {
        super.init(context)
        bottom_start.setOnClickListener(this)
        iv_share.setOnClickListener(this)
    }

    override fun updateStartImage() {
        super.updateStartImage()
        if (currentState == Jzvd.CURRENT_STATE_PLAYING) {
            bottom_start.setImageResource(R.drawable.jz_click_pause_selector)
        } else if (currentState == Jzvd.CURRENT_STATE_ERROR) {
            bottom_start.visibility = View.INVISIBLE
        } else if (currentState == Jzvd.CURRENT_STATE_AUTO_COMPLETE) {
            bottom_start.visibility = View.VISIBLE
            bottom_start.setImageResource(R.drawable.jz_click_replay_selector)
        } else {
            bottom_start.setImageResource(R.drawable.jz_click_play_selector)
        }
    }

    override fun onClick(v: View) {
        super.onClick(v)
        when (v) {
            bottom_start -> startButton.performClick()
            iv_share -> {
                toShare()
            }
            back -> {
                toBack()
            }
        }
    }

    private fun toShare() {

    }


    private fun toBack() {
        if (currentScreen == Jzvd.SCREEN_WINDOW_NORMAL) {
            if (context is Activity) (context as Activity).finish()
        } else Jzvd.backPress()
    }

    override fun getLayoutId(): Int {
        return R.layout.view_player
    }

    override fun setSystemTimeAndBattery() {
        batteryTimeLayout.visibility = View.GONE
        backButton.visibility = View.VISIBLE
    }

}
